public class Day13 {
    // 反转链表
    public ListNode reverseList(ListNode head) {
        if(head == null) return null;
        ListNode cur = head, prev = null, next = head.next;
        while(cur != null) {
            cur.next = prev;
            prev = cur;
            cur = next;
            if(cur != null) {
                next = cur.next;
            }
        }
        return prev;
    }

    // 找到链表环的入口
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head, fast = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast) break;
        }
        if(fast == null || fast.next == null) return null;
        ListNode cur1 = slow, cur2 = head;
        while(cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur2;
    }
}
